namespace ASPNET.StarterKit.IssueTracker
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using ASPNET.StarterKit.IssueTracker.UserInterfaceLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ProjectCategories : UserControl, IEditProjectControl
    {
        [AccessedThroughProperty("btnAdd")]
        private Button _btnAdd;
        [AccessedThroughProperty("btnDelete")]
        private Button _btnDelete;
        [AccessedThroughProperty("CustomValidator1")]
        private CustomValidator _CustomValidator1;
        [AccessedThroughProperty("dropCats")]
        private PickCategory _dropCats;
        [AccessedThroughProperty("lblError")]
        private Label _lblError;
        private int _ProjectId;
        [AccessedThroughProperty("txtName")]
        private TextBox _txtName;
        private object designerPlaceholderDeclaration;

        public ProjectCategories()
        {
            base.Init += new EventHandler(this.Page_Init);
            this._ProjectId = -1;
        }

        public void BindCategories()
        {
            this.dropCats.DataSource = new CategoryTree().GetCategoryTreeByProjectId(this.ProjectId);
            this.dropCats.DataBind();
        }

        public void btnAddCategory(object s, EventArgs e)
        {
            if (StringType.StrCmp(this.txtName.Text.Trim(), string.Empty, false) != 0)
            {
                Category newCat = new Category(this.ProjectId, this.dropCats.SelectedValue, this.txtName.Text);
                if (!newCat.Save())
                {
                    this.lblError.Text = "Could not save new category";
                }
                else
                {
                    this.txtName.Text = string.Empty;
                    this.BindCategories();
                }
            }
        }

        public void btnDeleteCategory(object s, EventArgs e)
        {
            if (this.dropCats.SelectedValue != 0)
            {
                if (!Category.DeleteCategory(this.dropCats.SelectedValue))
                {
                    this.lblError.Text = "Could not delete category";
                }
                else
                {
                    this.BindCategories();
                }
            }
        }

        public void Initialize()
        {
            this.BindCategories();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        public bool Update()
        {
            return this.Page.IsValid;
        }

        public void ValidateCategories(object s, ServerValidateEventArgs e)
        {
            if (this.dropCats.ItemCount == 1)
            {
                e.IsValid = false;
            }
            else
            {
                e.IsValid = true;
            }
        }

        int ASPNET.StarterKit.IssueTracker.UserInterfaceLayer.IEditProjectControl.ProjectId
        {
            get
            {
                return this._ProjectId;
            }
            set
            {
                this._ProjectId = value;
            }
        }

        protected virtual Button btnAdd
        {
            get
            {
                return this._btnAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click -= new EventHandler(this.btnAddCategory);
                }
                this._btnAdd = value;
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click += new EventHandler(this.btnAddCategory);
                }
            }
        }

        protected virtual Button btnDelete
        {
            get
            {
                return this._btnDelete;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnDelete != null)
                {
                    this._btnDelete.Click -= new EventHandler(this.btnDeleteCategory);
                }
                this._btnDelete = value;
                if (this._btnDelete != null)
                {
                    this._btnDelete.Click += new EventHandler(this.btnDeleteCategory);
                }
            }
        }

        protected virtual CustomValidator CustomValidator1
        {
            get
            {
                return this._CustomValidator1;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._CustomValidator1 != null)
                {
                    this._CustomValidator1.ServerValidate -= new ServerValidateEventHandler(this.ValidateCategories);
                }
                this._CustomValidator1 = value;
                if (this._CustomValidator1 != null)
                {
                    this._CustomValidator1.ServerValidate += new ServerValidateEventHandler(this.ValidateCategories);
                }
            }
        }

        protected virtual PickCategory dropCats
        {
            get
            {
                return this._dropCats;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._dropCats != null)
                {
                }
                this._dropCats = value;
                if (this._dropCats != null)
                {
                }
            }
        }

        protected virtual Label lblError
        {
            get
            {
                return this._lblError;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lblError != null)
                {
                }
                this._lblError = value;
                if (this._lblError != null)
                {
                }
            }
        }

        public int ProjectId
        {
            get
            {
                return this._ProjectId;
            }
            set
            {
                this._ProjectId = value;
            }
        }

        protected virtual TextBox txtName
        {
            get
            {
                return this._txtName;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._txtName != null)
                {
                }
                this._txtName = value;
                if (this._txtName != null)
                {
                }
            }
        }
    }
}

